package com.flyme.module.base.provider.controller;

import com.flyme.common.core.annotation.ApiRateLimit;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysApiRemoteService;
import com.flyme.module.base.client.entity.SystemApi;
import com.flyme.module.base.provider.service.SysApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author zyf
 */
@Api(tags = "接口资源管理")
@RestController
public class SysApiController extends BaseController<SysApiService, SystemApi> implements SysApiRemoteService {

    /**
     * Api列表
     *
     * @return
     */
    @ApiOperation(value = "获取接口分页列表", notes = "获取接口分页列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "pagerInfo", value = "当前页码", paramType = "form"), @ApiImplicitParam(name = "keyword", value = "查询字段", paramType = "form"),})
    @PostMapping("/api")
    @ApiRateLimit(limit = 10, interval = 1000)
    @Override
    public PqGrid api(@RequestParam(value = "pagerInfo") PagerInfo pagerInfo, @RequestParam(name = "keyword", required = false) String keyword) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.like("apiCode", keyword).or().like("apiName", keyword);
        cq.orderByAsc("id,priority");
        return bizService.pageList(cq);
    }


    /**
     * 获取Api资源
     *
     * @param apiId ApiId
     * @return 应用信息
     */
    @ApiOperation(value = "获取接口资源", notes = "获取接口资源")
    @ApiImplicitParams({@ApiImplicitParam(name = "apiId", required = true, value = "ApiId", paramType = "path"),})
    @ApiRateLimit(limit = 20, interval = 1000)
    @GetMapping("/api/{apiId}")
    @Override
    public ResultBody getApi(@PathVariable("apiId") Long apiId) {
        return ResultBody.success(bizService.getById(apiId));
    }

    /**
     * 添加接口资源
     *
     * @param apiCode   接口编码
     * @param apiName   接口名称
     * @param serviceId 服务ID
     * @param path      请求路径
     * @param status    是否启用
     * @param priority  优先级越小越靠前
     * @param apiDesc   描述
     * @return
     */
    @ApiOperation(value = "添加接口资源", notes = "添加接口资源")
    @ApiImplicitParams({@ApiImplicitParam(name = "apiCode", required = true, value = "接口编码", paramType = "form"), @ApiImplicitParam(name = "apiName", required = true, value = "接口名称", paramType = "form"), @ApiImplicitParam(name = "apiCategory", required = true, value = "接口分类", paramType = "form"), @ApiImplicitParam(name = "serviceId", required = true, value = "服务ID", paramType = "form"), @ApiImplicitParam(name = "path", required = false, value = "请求路径", paramType = "form"), @ApiImplicitParam(name = "status", required = true, defaultValue = "1", allowableValues = "0,1", value = "是否启用", paramType = "form"), @ApiImplicitParam(name = "priority", required = false, value = "优先级越小越靠前", paramType = "form"), @ApiImplicitParam(name = "apiDesc", required = false, value = "描述", paramType = "form"),})
    @PostMapping("/api/add")
    @Override
    public ResultBody addApi(@RequestParam(value = "apiCode") String apiCode, @RequestParam(value = "apiName") String apiName, @RequestParam(value = "apiCategory") String apiCategory, @RequestParam(value = "serviceId") String serviceId, @RequestParam(value = "path", required = false, defaultValue = "") String path, @RequestParam(value = "status", defaultValue = "1") Integer status, @RequestParam(value = "priority", required = false, defaultValue = "0") BigDecimal priority, @RequestParam(value = "apiDesc", required = false, defaultValue = "") String apiDesc) {
        SystemApi api = new SystemApi();
        api.setApiCode(apiCode);
        api.setApiName(apiName);
        api.setApiCategory(apiCategory);
        api.setServiceId(serviceId);
        api.setPath(path);
        api.setStatus(status);
        api.setPriority(priority);
        api.setApiDesc(apiDesc);
        boolean result = bizService.addApi(api);
        return ResultBody.success(result);
    }

    /**
     * 编辑接口资源
     *
     * @param apiId     接口ID
     * @param apiCode   接口编码
     * @param apiName   接口名称
     * @param serviceId 服务ID
     * @param path      请求路径
     * @param status    是否启用
     * @param priority  优先级越小越靠前
     * @param apiDesc   描述
     * @return
     */
    @ApiOperation(value = "编辑接口资源", notes = "编辑接口资源")
    @ApiImplicitParams({@ApiImplicitParam(name = "apiId", required = true, value = "接口Id", paramType = "form"), @ApiImplicitParam(name = "apiCode", required = true, value = "接口编码", paramType = "form"), @ApiImplicitParam(name = "apiName", required = true, value = "接口名称", paramType = "form"), @ApiImplicitParam(name = "apiCategory", required = true, value = "接口分类", paramType = "form"), @ApiImplicitParam(name = "serviceId", required = true, value = "服务ID", paramType = "form"), @ApiImplicitParam(name = "path", required = false, value = "请求路径", paramType = "form"), @ApiImplicitParam(name = "status", required = true, defaultValue = "1", allowableValues = "0,1", value = "是否启用", paramType = "form"), @ApiImplicitParam(name = "priority", required = false, value = "优先级越小越靠前", paramType = "form"), @ApiImplicitParam(name = "apiDesc", required = false, value = "描述", paramType = "form"),})
    @PostMapping("/api/update")
    @Override
    public ResultBody updateApi(@RequestParam("apiId") Long apiId, @RequestParam(value = "apiCode") String apiCode, @RequestParam(value = "apiName") String apiName, @RequestParam(value = "apiCategory") String apiCategory, @RequestParam(value = "serviceId") String serviceId, @RequestParam(value = "path", required = false, defaultValue = "") String path, @RequestParam(value = "status", defaultValue = "1") Integer status, @RequestParam(value = "priority", required = false, defaultValue = "0") BigDecimal priority, @RequestParam(value = "apiDesc", required = false, defaultValue = "") String apiDesc) {
        SystemApi api = new SystemApi();
        api.setId(apiId);
        api.setApiCode(apiCode);
        api.setApiName(apiName);
        api.setApiCategory(apiCategory);
        api.setServiceId(serviceId);
        api.setPath(path);
        api.setStatus(status);
        api.setPriority(priority);
        api.setApiDesc(apiDesc);
        boolean result = bizService.updateApi(api);
        return ResultBody.success(result);
    }

    /**
     * 更新状态
     *
     * @param apiId 接口ID
     * @return
     */
    @ApiOperation(value = "更新接口资源状态", notes = "更新接口资源状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "apiId", required = true, value = "ApiId", paramType = "form"), @ApiImplicitParam(name = "status", required = true, defaultValue = "1", allowableValues = "0,1", value = "是否启用", paramType = "form")})
    @PostMapping("/api/update/status")
    @Override
    public ResultBody updateStatus(@RequestParam("apiId") Long apiId, @RequestParam(value = "status", defaultValue = "1") Integer status) {
        boolean result = bizService.updateStatus(apiId, status);
        return ResultBody.success(result);
    }

    /**
     * 移除接口
     *
     * @param apiId ApiID
     * @return
     */
    @ApiOperation(value = "移除接口资源", notes = "移除接口资源")
    @ApiImplicitParams({@ApiImplicitParam(name = "apiId", required = true, value = "ApiId", paramType = "form"),})
    @PostMapping("/api/remove")
    @Override
    public ResultBody removeApi(@RequestParam("apiId") Long apiId) {
        boolean result = bizService.removeApi(apiId);
        return ResultBody.success(result);
    }
}
